package com.example.bootdemo.book.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.example.bootdemo.book.service.VueBookService;
import com.example.bootdemo.book.entity.VueBook;
import com.example.bootdemo.common.RestResult;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;

/**
 * 控制器
 */
@RestController
@RequestMapping("/api/book/vue-book")
public class VueBookController {
    @Autowired
    private VueBookService vueBookService;

    /**
     * 新增VueBook
     */
    @PostMapping("/save")
    public Boolean save(@RequestBody VueBook vueBook) {
        return vueBookService.save(vueBook);
    }

    /**
     * 删除VueBook
     */
    @PostMapping("/delete")
    public Boolean delete(@RequestBody List<String> ids) {
        return vueBookService.removeBatchByIds(ids);
    }

    /**
     * 修改VueBook
     */
    @PostMapping("/update")
    public Boolean update(@RequestBody VueBook vueBook) {
        return vueBookService.updateById(vueBook);
    }

    /**
     * 主键查询VueBook
     */
    @GetMapping("/{id}")
    public RestResult<VueBook> findById(@PathVariable Long id) {
        return RestResult.ok(vueBookService.getById(id));
    }

    /**
     * 分页查询VueBook
     */
    @GetMapping("/page")
    public RestResult<Page<VueBook>> pageQuery(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<VueBook> bookPage = new Page<>(pageNum, pageSize);
        bookPage.setOrders(Lists.newArrayList(OrderItem.desc("create_time")));
        return RestResult.ok(vueBookService.page(bookPage));
    }

    /**
     * 列表查询VueBook
     */
    @GetMapping("/list")
    public List<VueBook> listAll() {
        return vueBookService.list();
    }
}
